package com.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionManager;

import javax.sql.DataSource;

/**
 * @ClassName TransactionManagerConfig
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/1 17:34
 * @Version 1.0
 **/
@Component
@Configuration
public class TransactionManagerConfig {
    @Autowired
    DataSource dataSource;

    /**
     * 获取平台事务管理器
     * @return 平台事务管理器对象
     */
    @Bean("asd")
    public DataSourceTransactionManager dataSourceTransactionManager(){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    @Bean("jdbcTemplate")
    @Scope
    public JdbcTemplate getJdbcTemplate() {
        return new JdbcTemplate(dataSource);
    }
}
